<?php

namespace App\Http\Controllers\admin;

use App\Column;
use App\Http\Controllers\Controller;
use App\Tag;
use App\Http\Requests\Addtags;
use App\Http\Requests\Modeifylabel;
use Illuminate\Http\Request;

class Tags_mangement extends Controller
{
    public function addtags(Addtags $Request)
    {
        $req = $Request->all();
        if (Tag::where('tag_name', $req['title'])->first()) {
            $data = [
                'code' => 100,
                'msg' => '标题已经存在',
                'data' => '标题已经存在'
            ];
            return response()->json($data);
        } else {
            $tag = new Tag();
            $tag->tag_name = $req['title'];
            $tag->column_id = $req['ColumnID'];
            $tag->created_at = date('Y-m-d H:i:s', time());
            $tag->save();
            $data = [
                'code' => 200,
                'msg' => '成功',
                'data' => null
            ];
            return response()->json($data);
        }
    }

    public function modifylabel(Modeifylabel $Request)
    {
        $req = $Request->all();
        if (Tag::where('id', $req['id'])->first() == null) {
            $data = [
                'code' => 100,
                'msg' => 'id不存在',
                'data' => 'id不存在'
            ];
            return response()->json($data);
        } else {
            $ta = Tag::where('tag_name', $req['title'])->where('id', '!=', $req['id'])->first();
            if ($ta) {
                $data = [
                    'code' => 100,
                    'msg' => '标题已经存在',
                    'data' => '标题已经存在'
                ];
                return response()->json($data);
            } else {
                $tag = Tag::where('id', $req['id'])->first();
                $tag->tag_name = $req['title'];
                $tag->column_id = $req['ColumnID'];
                $tag->updated_at = date('Y-m-d H:i:s', time());
                $tag->save();
                $data = [
                    'code' => 200,
                    'msg' => '成功',
                    'data' => null
                ];
                return response()->json($data);
            }
        }
    }

    public function deletelabel($id)
    {

        $tag = Tag::find($id);
        if ($tag == null) {
            $data = [
                'code' => 100,
                'msg' => '不存在此标签',
                'data' => '不存在此标签'
            ];
            return response()->json($data);
        } else {
            $bool = $tag->delete();
            $data = [
                'code' => 200,
                'msg' => '成功',
                'data' => null
            ];
            return response()->json($data);
        }
        //$tag = Tag::destroy($id);
        //dd($tag);
    }

    public function query($id)
    {
        if ($id == 0) {
            $tags = Tag::orderBy('updated_at','desc')->paginate(5);
            foreach ($tags as $key => $tag) {
                $column = Column::where('id', $tag->column_id)->first();
                $tags[$key]->column_name = $column->column_name;
                unset($tags[$key]['column_id']);
            }
            $data = [
                'code' => 200,
                'msg' => '成功',
                'data' => $tags
            ];
            return response()->json($data);
        } else {
            $tags = Tag::where('column_id', $id)->orderBy('updated_at','desc')->paginate(5);
            $column = Column::where('id', $id)->first();
            foreach ($tags as $key => $tag) {
                $tags[$key]->column_name = $column->column_name;
            }
            $data = [
                'code' => 200,
                'msg' => '成功',
                'data' => $tags
            ];
            return response()->json($data);
        }
    }
}
